package com.fang.gateway.security.handler;

import com.fang.common.common.CommonResult;
import com.fang.common.exception.ExceptionCode;
import com.fang.common.util.CommonResultUtil;
import com.fang.gateway.security.utils.HandlerUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Description 匿名用户访问(未登录)无权限资源时的异常
 * @Author Bernie
 * @Date 2024/4/23 10:45
 **/
@Component
public class FluxAuthenticationEntryPoint implements ServerAuthenticationEntryPoint {

    @Override
    public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException ex) {
        CommonResult result = CommonResultUtil.failed(ExceptionCode.USER_NOT_LOGIN);
        return HandlerUtils.writeWith(exchange, result);
    }
}
